package com.example.yy.fragementtest318yy.model;




import com.example.yy.fragementtest318yy.bean.TwareBean;
import com.example.yy.fragementtest318yy.iface.TwareListener;
import com.example.yy.fragementtest318yy.iface.Twareiface;
import com.example.yy.fragementtest318yy.service.TwareService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by yy on 2017/4/25.
 */

public class TwareModel implements Twareiface {
    //1
    private Retrofit retrofit;
    private String BASEURL =
            "http://amicool.neusoft.edu.cn/";

    public TwareModel() {
        retrofit = new Retrofit.Builder().baseUrl(BASEURL).addConverterFactory(GsonConverterFactory.create()).build();
    }

    @Override
    public void getResultList(String tware, int page, String sessionID, final TwareListener twareListener) {
        //2
        TwareService twareService =
                retrofit.create(TwareService.class);
        Call<List<TwareBean>> call = twareService.getResult(tware,page,sessionID);
        call.enqueue(new Callback<List<TwareBean>>() {
            @Override
            public void onResponse(Call<List<TwareBean>> call, Response<List<TwareBean>> response) {
                if(response.isSuccessful()&&response!=null){
                    twareListener.onResponse(response.body());
                }else
                    twareListener.onFail("OnResponseFail");
            }

            @Override
            public void onFailure(Call<List<TwareBean>> call, Throwable t) {
                twareListener.onFail(t.toString());
            }
        });
    }
}

